<?php
namespace Frieza\Modules\CommunityMcn\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\tools\ImgUrl;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Doraemon\service\CommunityMcn\GoodsRecommender as sGoodsRecommender;
use Doraemon\model\CommunityMcn\GoodsRecommender as mGoodsRecommender;

class RecommenderListController extends Controller{

    public function index(Request $request, Content $content){
        $params = $request->all();
        $page = $request->input('page', 1);
        $pageSize = $request->input('pageSize', 20);
        $statusType = $request->input('status', 0);
        $where = [];
        if($statusType){
            $where = mGoodsRecommender::getStatusWhere($statusType);
        }else{
            $where['status'] = mGoodsRecommender::STATUS_NORMAL;
        }
        if($params['reward_type']){
            $where['reward_type'] = $params['reward_type'];
        }
        if($params['title']){
            $where['title[~]'] = '%'.$params['title'].'%';
        }
        if($params['start_time']['start']){
            $where['start_time[>=]'] = strtotime($params['start_time']['start']);
        }
        if($params['start_time']['end']){
            $where['start_time[<=]'] = strtotime($params['start_time']['end']);
        }
        if($params['end_time']['start']){
            $where['end_time[>=]'] = strtotime($params['end_time']['start']);
        }
        if($params['end_time']['end']){
            $where['end_time[<=]'] = strtotime($params['end_time']['end']);
        }
        $limit = [($page - 1) * $pageSize, $pageSize];
        $order = ['create_time' => 'DESC'];
        $data = sGoodsRecommender::getList($where, $limit, $order);
        $list = self::formatData($data['list']);
        $gridModel = new pGridDataModel($list, $data['total'], ['perPage' => $pageSize]);

        $listGrid = Admin::grid($gridModel, function (Grid $grid){
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->column(1 / 2, function ($filter) {
                    $filter->equal('title', '活动名称')->placeholder('活动名称');
                    $filter->equal('reward_type', '奖励类型')->radio(mGoodsRecommender::REWARD_TYPE_LIST);
                });
                $filter->column(1 / 2, function ($filter) {
                    $filter->dropDown('start_time', [
                        'start_time' => '开始时间',
                    ])->betweenDatetime();
                    $filter->dropDown('end_time', [
                        'end_time' => '结束时间',
                    ])->betweenDatetime();
                });
            });

            $grid->column('id')->hide();
            $grid->column('title', '活动名称');
            $grid->column('pic_id', '活动图')->display(function() {
                return "<img src='".ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $this->pic_id, ImgUrl::IMAGE_SIZE_OP_ORI)."' width='50' />";
            });;
            $grid->column('time', '开始-结束时间');
            $grid->column('reward_type', '奖品类型');
            $grid->column('status', '状态');
            $grid->column('activity_tag', '标签');
            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableEdit();
                $actions->disableView();
                $id = $actions->row->id;
                $actions->prepend('<a class="fa fa-indent reload-data" href="/admin/communityMcn/recommenderInfo/' . $id . '/edit" >活动详情</a><br>');
                $actions->prepend('<a class="fa fa-indent reload-data" href="/admin/communityMcn/joinList/' . $id . '/list" >参与内容</a><br>');
            });
        });
        $tab = new Tab();
        switch ($statusType){
            case 3:
                $tab->addLink('全部', '/admin/communityMcn/recommenderInfo/index');
                $tab->add('进行中', $listGrid->render(), true);
                $tab->addLink('已结束', '/admin/communityMcn/recommenderInfo/index?status=' . 6);
                break;
            case 6:
                $tab->addLink('全部', '/admin/communityMcn/recommenderInfo/index');
                $tab->addLink('进行中', '/admin/communityMcn/recommenderInfo/index?status=' . 3);
                $tab->add('已结束', $listGrid->render(), true);
                break;
            default:
                $tab->add('全部', $listGrid->render(), true);
                $tab->addLink('进行中', '/admin/communityMcn/recommenderInfo/index?status=' . 3);
                $tab->addLink('已结束', '/admin/communityMcn/recommenderInfo/index?status=' . 6);
                break;
        }
        $content->row($tab);
        return $content
            ->header('MCN项目管理')
            ->breadcrumb(['text' => 'MCN项目管理']);
    }

    public function delIndex($id){
        $rs = mGoodsRecommender::updateData($id, [
            "status" => mGoodsRecommender::STATUS_DELETE,
        ]);

        if($rs) {
            return response()->json([
                'status' => true,
                'message' => "删除成功",
            ]);
        } else {
            return response()->json([
                'status' => false,
                'message' => "删除失败",
            ]);
        }
    }
    private static function formatData($data)
    {
        $formatData = [];
        foreach ($data as $key => $recommenderInfo){
            $formatData[$key]['id'] = $recommenderInfo['id'];
            $formatData[$key]['title'] = $recommenderInfo['title'];
            $formatData[$key]['pic_id'] = $recommenderInfo['pic_id'];
            $formatData[$key]['time'] = date('Y.m.d',$recommenderInfo['start_time']).'-'.date('Y.m.d',$recommenderInfo['end_time']);
            $formatData[$key]['reward_type'] = mGoodsRecommender::REWARD_TYPE_LIST[$recommenderInfo['reward_type']];
            $formatData[$key]['status'] = mGoodsRecommender::STATUS_LIST[sGoodsRecommender::getStatus($recommenderInfo['start_time'],$recommenderInfo['end_time'])];
            $formatData[$key]['activity_tag'] = $recommenderInfo['activity_tag'];
        }
        return $formatData;
    }
}